package com.pkk.penguin.frame.know.java.driven.profile;

import com.alibaba.fastjson.JSONObject;
import com.pkk.penguin.frame.know.java.driven.profile.bean.ProfileBean;
import java.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * 主测试类
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-10-13 17:07
 **/
@ComponentScan("com.pkk.penguin.frame.know.java.driven.profile")
public class ProfileMainTest {

  public static void main(String[] args) {
    //模拟有参构造器
//    ApplicationContext ApplicationContext = mode1();
    ApplicationContext applicationContext = mode2();

    System.out
        .println("当前环境:" + Arrays.toString(applicationContext.getEnvironment().getActiveProfiles()));

    //所有bean
    final String[] beanNamesForType = applicationContext.getBeanNamesForType(ProfileBean.class);
    Arrays.stream(beanNamesForType).forEach(a -> System.out.println(JSONObject.toJSONString(a)));

    //所有bean
    final String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
    Arrays.stream(beanDefinitionNames).forEach(a -> System.out.println(JSONObject.toJSONString(a)));
  }


  /**
   * 方式一
   *
   * @return org.springframework.context.annotation.AnnotationConfigApplicationContext
   * @Param
   * @author peikunkun
   * @date 2020/10/14 0014 上午 10:35
   * @since
   */
  public static AnnotationConfigApplicationContext mode1() {
    //模拟有参构造器
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    //设置环境
    annotationConfigApplicationContext.getEnvironment().setActiveProfiles("dev");
    //注册配置
    annotationConfigApplicationContext.register(ProfileMainTest.class);
    //刷新配置
    annotationConfigApplicationContext.refresh();

    return annotationConfigApplicationContext;
  }

  /**
   * 方式二【设置环境变量-Dspring.profiles.active=dev】
   *
   * @return org.springframework.context.annotation.AnnotationConfigApplicationContext
   * @Param
   * @author peikunkun
   * @date 2020/10/14 0014 上午 10:35
   * @since
   */
  public static AnnotationConfigApplicationContext mode2() {
    //模拟有参构造器
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
        ProfileMainTest.class);

    return annotationConfigApplicationContext;
  }

}
